package com.xdf.xzymanagementsystem.controller;

import com.xdf.xzymanagementsystem.entity.ResponseResult;
import com.xdf.xzymanagementsystem.entity.SysMenu;
import com.xdf.xzymanagementsystem.entity.SysPermission;
import com.xdf.xzymanagementsystem.entity.SysRole;
import com.xdf.xzymanagementsystem.enums.ResponseEnum;
import com.xdf.xzymanagementsystem.service.SysMenuService;
import com.xdf.xzymanagementsystem.service.SysPermissionService;
import com.xdf.xzymanagementsystem.service.SysRoleGroupDtlService;
import com.xdf.xzymanagementsystem.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description : 自动生成的控制层
 * @Author : 张柯
 * @Date : 2020-12-31 14:42:38
 */
@Api(tags = "角色表")
@CrossOrigin
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService service;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysRoleGroupDtlService roleGroupDtlService;

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(SysRole record) throws Exception {
        record.setId(null);
        Assert.state(service.insertSelective(record) > 0, ResponseEnum.SAVE_DATA_ERROR.getName());
        Integer sysRoleId = record.getId();
        List<SysMenu> sysMenuList = menuService.listQuery(SysMenu.builder().build());
        if (CollectionUtils.isNotEmpty(sysMenuList)) {
            permissionService.multiInsert(sysMenuList.stream().map(x ->
                    SysPermission.builder()
                            .sysRoleId(sysRoleId)
                            .sysMenuId(x.getId())
                            .build()
            ).collect(Collectors.toList()));
        }
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 选择修改
     * @Return : SysRole
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(SysRole record) throws Exception {
        if (record.getRoleAdminSign() == null) {
            record.setRoleAdminSign(false);
        }
        if (record.getRoleWorkshopOnly() == null) {
            record.setRoleWorkshopOnly(false);
        }
        service.updateByPrimaryKeySelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 根据主键查询
     * @Return : SysRole
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public SysRole selectByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<SysRole> query(SysRole record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequestMapping(value = "/deleteByPk", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Void> deleteByPrimaryKey(@RequestParam("id") Integer id) throws Exception {
        permissionService.deleteByRoleId(id);
        roleGroupDtlService.deleteByRoleId(id);
        service.deleteByPrimaryKey(id);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }
}